#include "../server.hpp"

namespace wyl
{

    class EchoServer
    {
    private:
        TcpServer _server;
        void OnMessage(const PtrConnection &conn, Buffer *buf)
        {
            DLOG("%s", buf->ReadPosition());
            buf->MoveReadOffset(buf->ReadAbleSize());
            std::string str = "hello world";
            conn->Send(str.c_str(), str.size());
        }
        void OnConnected(const PtrConnection &conn)
        {
            DLOG("NEW CONNECTION:%p", conn.get());
        }
        void OnClose(const PtrConnection &conn)
        {
             DLOG("CLOSE CONNECTION:%p", conn.get());
        }
    public:
        EchoServer(int port) :_server(port)
        {
            _server.SetThreadCount(2);
            _server.EnableInactiveRelease(3);
            _server.SetMessageCallBack(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
            _server.SetConnectedCallBack(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
            _server.SetClosedCallBack(std::bind(&EchoServer::OnClose,this,std::placeholders::_1));
        }
        void Start()
        {
          _server.Start();
        }
    };
}
